package org.xcorpio.java8.chapter01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PassingCode {

    public interface Predicate<T> {
        boolean test(T t);
    }

    static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        ArrayList<Apple> result = new ArrayList<Apple>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    static <T> Collection<T> filter(Collection<T> c, Predicate<T> p) {
        return c;
    }

    public static void main(String[] args) {
        ArrayList<Apple> apples = new ArrayList<Apple>();
        filterApples(apples, Apple::isGreenApple);
        filterApples(apples, Apple::isHeavyApple);
        filterApples(apples, (Apple a) -> "green".equals(a.getColor()));
        filterApples(apples, (Apple a) -> a.getWeight() > 150);
        filterApples(apples, (Apple a) -> a.getWeight() < 80 || "brown".equals(a.getColor()));
    }
}

class Apple {

    private String color;
    private int weight;

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 150;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}
